/**

 * Definition for a binary tree node.

 * public class TreeNode {

 *     int val;

 *     TreeNode left;

 *     TreeNode right;

 *     TreeNode() {}

 *     TreeNode(int val) { this.val = val; }

 *     TreeNode(int val, TreeNode left, TreeNode right) {

 *         this.val = val;

 *         this.left = left;

 *         this.right = right;

 *     }

 * }

 */

class Solution {

     int getHeight(TreeNode root) {

        if(root == null) {

            return 0;

        }

        int left = getHeight(root.left);

        int right = getHeight(root.right);

        if(left>=0 && right>=0 && Math.abs(left-right)<=1) {

            return Math.max(left,right)+1;

        }else {

            return -1;

        }

    }



    public boolean isBalanced(TreeNode root) {

        return getHeight(root)>=0;

    }

}



